<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\video\logic;
use think\facade\Cache;
class Pushlogic{

	/**
     * 关注的人的视频推送
     * @param  int $uid  用户id
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function follow_play_push($uid, $page_size){
        $cacheName = 'uid_follow_push_temp_'.$uid;
        if(!Cache::get('uid_follow_push_temp_'.$uid)){
            //查询关注的人的 列表
            $map = [];
            $map['uid'] = $uid;
            $list = model('user_center/UcMemberFollow', 'model\user') -> where($map) -> select();
           
            //临时推送池子（1小时缓存）
            if(!empty($list->toArray())){
                Cache::set('uid_follow_push_temp_'.$uid, $list, 60*60);
            }
        }
        $rel_list = $data = [];
        $list_a = Cache::get('uid_follow_push_temp_'.$uid) ? Cache::get('uid_follow_push_temp_'.$uid) : [];
        foreach ($list_a as $key => $value) {
            $temp_list  = datacache('video/VideoPlay') -> getFollowPushFilter($uid, $value['follow_uid']);
            if(!empty($temp_list)){
				foreach($temp_list as $val){
					$rel_list[] = $val;
				}
			}
            // if(count($rel_list) >= $page_size) break;
        }
        //标准化输出
        $data = controller('Videologic', 'logic') -> returnBaceArray($uid, $rel_list);
        //dump($data);die;
        return array_out(200, 'success', $data);
    }

    /**
     * 关注的人的视频推送
     * @param  int $uid  用户id
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function follow_play_push_list($uid, $num = 15, $cache = false){
        $cacheName = 'follow_play_push_list_temp_'.$uid;
        if(!Cache::get($cacheName)  || $cache){
            //查询关注的人的 列表
            $map = [];
            $map['uid'] = $uid;
            $list = model('VideoPlay', 'model') -> getFollowListByUid($uid);
            
            Cache::set($cacheName, $list['data'], 60*60);
        }

        $data = Cache::get($cacheName);//dump(count($data));
        $i = 0;
        $lista = [];
        foreach ($data as $key => $value) {
            $lista[] = $value;//dump($value['uid']);
            //推送数据 缓存池剔除
            unset($data[$key]);
            $i++;
            if($i == $num) break;
        }

        $result_list = controller('Videologic', 'logic')->returnBaceArray($uid, $lista);
        
        
        //缓存池子 重新赋值
        Cache::set($cacheName, $data, 60*60);//默认缓存1天

        return array_out(200, 'success', (array)$result_list);
    }


}